package cn.accp.mvc.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.accp.mvc.entity.allEntity;
import cn.accp.mvc.service.HouseService;


@Controller
@RequestMapping("/page")
public class HouseController {
	@Autowired
	private HouseService houseService;
	public void setHouseService(HouseService houseService) {
		this.houseService = houseService;
	}
	@RequestMapping("/gethouseList")
	public String gethouseList(String opr,HttpServletRequest request,HttpSession session){
		if(opr == null){
			opr = (String)session.getAttribute("opr");
		}
		List<allEntity> houseList = null;
		int pageSize = 3;			
		int pageIndex = 1;
		String dijiye = (String)request.getParameter("dijiye");
		if(dijiye != null){
			pageIndex =Integer.parseInt(dijiye);		
		}
		session.setAttribute("pageIndex", pageIndex);
		houseList = houseService.findHouseByPage(pageIndex,pageSize);
		int total = houseService.houseCount(pageSize);		
		int[] yeshu = new int[total];
		for (int i = 0; i < total; i++) {
			yeshu[i] = i+1;
		}
		session.setAttribute("yeshu", yeshu);
		session.setAttribute("houseList", houseList);
		session.setAttribute("total", total);
		if(opr.equals("guanli")){	
			return "redirect:/page/guanli.jsp";
		}else if (opr.equals("list")) {
			return "redirect:/page/list.jsp";
		}else {
			return "redirect:/page/login.jsp";
		}
	}
	@RequestMapping("/houseActionInfo")
	public String houseActionInfo(HttpSession session){
		String opr = (String)session.getAttribute("opr");
		if(opr.equals("fabu")){
			if(session.getAttribute("house") != null){
				session.removeAttribute("house");
			}
			session.setAttribute("oprType", "立即发布");
			return "redirect:/page/fabu.jsp";
		}else if(opr.equals("update")){		
			session.setAttribute("oprType", "立即修改");
			return "redirect:/page/fabu.jsp";
		}else if (opr.equals("details")) {
			String id = (String)session.getAttribute("id");
			allEntity  house = houseService.getHouse(Integer.parseInt(id));
			session.setAttribute("house", house);
			return "redirect:/page/details.jsp";
		}else {
			return "redirect:/page/login.jsp";
		}
	}
}
